package com.bbva.gptf.back.dominio;

import java.math.BigDecimal;
import java.util.List;

import javax.persistence.NoResultException;

import org.apache.commons.lang.StringUtils;
import org.springframework.roo.addon.dbre.RooDbManaged;
import org.springframework.roo.addon.entity.RooEntity;
import org.springframework.roo.addon.javabean.RooJavaBean;
import org.springframework.roo.addon.tostring.RooToString;

import com.bbva.jee.arq.spring.core.log.I18nLog;
import com.bbva.jee.arq.spring.core.log.I18nLogFactory;

@RooJavaBean
@RooToString
@RooEntity(versionField = "", table = "TGPT027_SALDO", schema = "DBSGPT")
@RooDbManaged(automaticallyDelete = true)
public class Tgpt027Saldo {
	private final static I18nLog log = I18nLogFactory
			.getLogI18n(Tgpt027Saldo.class);

	public static BigDecimal obtieneSaldo(final List<BigDecimal> contratos,
			final String tipoMercado) {
		try {
			if (contratos == null || StringUtils.isBlank(tipoMercado)) {
				return BigDecimal.ZERO;
			}
			final StringBuffer qlString = new StringBuffer("SELECT ");
			if ("MC".equalsIgnoreCase(tipoMercado)) {

				qlString.append("SUM(");
				qlString.append("NVL(s.imEfectivoHoy,0)");
				qlString.append("+");
				qlString.append("NVL(s.imEfectivoV24,0)");
				qlString.append("+");
				qlString.append("NVL(s.imEfectivoV48,0)");
				qlString.append("+");
				qlString.append("NVL(s.imEfectivoV72,0)");
				qlString.append("+");
				qlString.append("NVL(s.imEfectivoV96,0)");
				//qlString.append(") * NVL(s.tgpt016Contrato.pcImpRv,0) ) / 100 ");
				qlString.append(") / 100 ");
			} else {
				qlString.append("SUM(");
				qlString.append("NVL(s.imEfectivoHoy,0)");
				qlString.append("+");
				qlString.append("NVL(s.imEfectivoV24,0)");
				qlString.append("+");
				qlString.append("NVL(s.imEfectivoV48,0)");
				qlString.append("+");
				qlString.append("NVL(s.imEfectivoV72,0)");
				qlString.append("+");
				qlString.append("NVL(s.imEfectivoV96,0)");
				//qlString.append(") *(100 -  NVL(s.tgpt016Contrato.pcImpRv,0)) ) / 100 ");
				qlString.append(") / 100 ");
			}
			qlString.append("FROM Tgpt027Saldo s ");
			qlString.append("WHERE s.tgpt016Contrato.cdContrato in (:contratos) ");
			log.info("Consulta: " + qlString.toString());
			final BigDecimal saldo = entityManager()
					.createQuery(qlString.toString(), BigDecimal.class)
					.setParameter("contratos", contratos).getSingleResult();

			return saldo != null ? saldo : BigDecimal.ZERO;
		} catch (final NoResultException nre) {
			return BigDecimal.ZERO;
		} catch (final Exception e) {
			return BigDecimal.ZERO;
		}
	}

	public static BigDecimal obtieneSaldoHoy(final List<BigDecimal> contratos,
			final String tipoMercado) {
		try {
			if (contratos == null || StringUtils.isBlank(tipoMercado)) {
				return BigDecimal.ZERO;
			}
			final StringBuffer qlString = new StringBuffer("SELECT ");
			qlString.append("SUM(NVL(s.imEfectivoHoy,0)) / 100 ");
			/*
			if ("MC".equalsIgnoreCase(tipoMercado)) {
				qlString.append("SUM(NVL(s.imEfectivoHoy,0) * NVL(s.tgpt016Contrato.pcImpRv,0) ) / 100 ");
			} else {
				qlString.append("SUM(NVL(s.imEfectivoHoy,0) * (100 - NVL(s.tgpt016Contrato.pcImpRv,0) )) / 100 ");
			}
			*/
			qlString.append("FROM Tgpt027Saldo s ");
			qlString.append("WHERE s.tgpt016Contrato.cdContrato in (:contratos) ");
			log.info("Consulta: " + qlString.toString());
			final BigDecimal saldo = entityManager()
					.createQuery(qlString.toString(), BigDecimal.class)
					.setParameter("contratos", contratos).getSingleResult();
			return saldo != null ? saldo : BigDecimal.ZERO;
		} catch (final NoResultException nre) {
			return BigDecimal.ZERO;
		} catch (final Exception e) {
			return BigDecimal.ZERO;
		}
	}
}
